package com.isoftstone.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.isoftstone.dao.EducateDAO;
import com.isoftstone.pojo.Educate;
import com.isoftstone.service.EducateService;

@Controller
public class EducateController {

	@Autowired
	private EducateService educateService;

	@RequestMapping("/listeducate")
	public String list(Model model,String educate) {
		List<Educate> list = educateService.getAllEducate(educate.equals("1"));
		model.addAttribute("list", list);
		return "listeducate";
	}
	

	@RequestMapping("/detaileducate")
	public String detail(Model model, String id) {
		Educate educate = educateService.getEducateById(Integer.parseInt(id));
		model.addAttribute("educate", educate);
		System.out.println(educate.getSummarize());
		return "detaileducate";
	}


	
	@RequestMapping(path ="/updateeducate",method = RequestMethod.POST)
	public String updatepost(Model model,String id, Educate educate) {
		java.util.Date dt = new java.util.Date();
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createtime = sdf.format(dt);
		educate.setCreatetime(createtime);
		educateService.update(educate);
		Educate educates = educateService.getEducateById(Integer.parseInt(id));
		model.addAttribute("educate", educates);
		return "updateeducate";
	}
	
	@RequestMapping(path ="/updateeducate",method = RequestMethod.GET)
	public String updateget(Model model,String id, Educate educate) {
		java.util.Date dt = new java.util.Date();
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createtime = sdf.format(dt);
		educate.setCreatetime(createtime);
		educateService.update(educate);
		Educate educates = educateService.getEducateById(Integer.parseInt(id));
		model.addAttribute("educate", educates);
		return "updateeducate";
	}
	
	@RequestMapping(path = "/addeducate",method = RequestMethod.POST)
	public String register4Post(Model model, Educate educate) {
		if (educate.getName() != null&&educate.getPurpose()!=null) {
			java.util.Date dt = new java.util.Date();
			java.text.SimpleDateFormat sdf =new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String createtime = sdf.format(dt);
			educate.setCreatetime(createtime);
			int i = educateService.addEducate(educate);
		}
		return "addeducate";
	}
	
	@RequestMapping(path = "/addeducate",method = RequestMethod.GET)
	public String register4Get(Model model, Educate educate) {
		if (educate.getName() != null&&educate.getPurpose()!=null) {
			java.util.Date dt = new java.util.Date();
			java.text.SimpleDateFormat sdf =new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String createtime = sdf.format(dt);
			educate.setCreatetime(createtime);
			int a;
			int i = educateService.addEducate(educate);
		}
		return "addeducate";
	}
	
	@RequestMapping("/deleteeducate")
	public String deleteeducate(Model model,String id) {
		Educate educate = educateService.getEducateById(Integer.parseInt(id));
		boolean flag = educate.isEducate();
		int i = educateService.remove(Integer.parseInt(id));
		List<Educate> list = educateService.getAllEducate(flag);
		model.addAttribute("list", list);
		return "listeducate";
	}
}
